package com.example.mycarcalling.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.example.mycarcalling.bean.Application;
import com.example.mycarcalling.bean.Order;
import com.example.mycarcalling.service.ApplicationService;
import com.example.mycarcalling.service.UserService;
import com.example.mycarcalling.utils.BaseResponse;
import com.example.mycarcalling.utils.ResponseResult;
import com.example.mycarcalling.vo.DriverApplication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@BaseResponse
@RequestMapping(value = "/application")
public class ApplicationController {
    
    @Resource
    private ApplicationService applicationService;

    @Resource
    private UserService userService;

    /**
     * 提交申请
     * @param driverApplication
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/applicationForDriver")
    public ResponseResult applicationForDriver(@RequestBody DriverApplication driverApplication, HttpServletRequest request){
        Long applicationId = null;
        Application application = new Application();
        try {
            application.setApplicationUser(StpUtil.getLoginIdAsLong());
            application.setApplicationStatus(0);

            application.setColor(driverApplication.getColor());
            application.setCarModel(driverApplication.getCarModel());
            application.setLicensePlate(driverApplication.getLicensePlate());
            application.setPassengerNumberPermit(driverApplication.getPassengerNumberPermit());

            application.setImgIdentityCard(driverApplication.getPictureNameList().get(0));
            application.setImgDrivingLicense(driverApplication.getPictureNameList().get(1));
            application.setImgDrivingPermitPerson(driverApplication.getPictureNameList().get(2));
            application.setImgDriverPermitCar(driverApplication.getPictureNameList().get(3));
            application.setImgCar(driverApplication.getPictureNameList().get(4));

            applicationId = applicationService.saveApplication(application);
            if(applicationId == null){
                throw new Exception("申请数据异常");
            }
            return new ResponseResult(200,"success",applicationId);
        }catch (NotLoginException e){
            return new ResponseResult(503,"failed",e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,"failed",e.getMessage());
        }
    }

    /**
     * 获取司机车辆信息
     * @param driverId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "getDriverCarInfo/{driverId}")
    public ResponseResult getDriverCarInfo(@PathVariable("driverId") Long driverId){
        Application application = null;
        try {

            StpUtil.getLoginIdAsLong();
            application = applicationService.getDriverApplication(driverId);
            if(null==application){
                throw new Exception("获取司机车辆信息失败");
            }
            return new ResponseResult(200,"success",application);

        }catch (NotLoginException e){
            return new ResponseResult(503,"failed",e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,"failed",e.getMessage());
        }
    }

    /**
     * 获取司机车辆信息
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "getDriverApplication")
    public ResponseResult getDriverApplication(){
        Application application = null;
        try {

            application = applicationService.getDriverApplication(StpUtil.getLoginIdAsLong());
            if(null==application){
                throw new Exception("获取司机车辆信息失败");
            }
            DriverApplication driverApplication = new DriverApplication();
            driverApplication.setColor(application.getColor());
            driverApplication.setCarModel(application.getCarModel());
            driverApplication.setLicensePlate(application.getLicensePlate());
            driverApplication.setPassengerNumberPermit(application.getPassengerNumberPermit());
            driverApplication.setStatus(application.getApplicationStatus());
            List<String> list = new ArrayList<>();
            list.add(application.getImgIdentityCard());
            list.add(application.getImgDrivingLicense());
            list.add(application.getImgDrivingPermitPerson());
            list.add(application.getImgDriverPermitCar());
            list.add(application.getImgCar());
            driverApplication.setPictureNameList(list);
            return new ResponseResult(200,"success",driverApplication);

        }catch (NotLoginException e){
            return new ResponseResult(503,"failed",null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,"failed",null);
        }
    }

}
